package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xin.di.uav.applet.mapper.ServiceAreaMapper;
import com.xin.di.uav.applet.request.DeliveryPointsAddParam;
import com.xin.di.uav.applet.request.DeliveryPointsEditParam;
import com.xin.di.uav.applet.resp.DeliveryPointsResp;
import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.applet.service.DeliveryPointsService;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.operate.mapper.CustomDeliveryPointMapper;
import com.xin.di.uav.operate.po.DeliveryPointPO;
import com.xin.di.uav.operate.po.PlacementPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class DeliveryPointsServiceImpl implements DeliveryPointsService {

    @Autowired
    private CustomDeliveryPointMapper customDeliveryPointMapper;

    @Autowired
    private ServiceAreaMapper serviceAreaMapper;

    @Override
    public boolean add(DeliveryPointsAddParam deliveryPointsAddParam) {
        DeliveryPointPO deliveryPointPO = BeanUtil.copyProperties(deliveryPointsAddParam, DeliveryPointPO.class);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        deliveryPointPO.setId(id);
        deliveryPointPO.setScenicSpotId(deliveryPointsAddParam.getServiceAreasId());
        return customDeliveryPointMapper.insert(deliveryPointPO) > 0;
    }

    @Override
    public boolean edit(DeliveryPointsEditParam deliveryPointsEditParam) {
        PlacementPoint placementPoint = customDeliveryPointMapper.selectById(deliveryPointsEditParam.getId());
        if (placementPoint == null) {
            throw new BusinessException("记录不存在");
        }
        placementPoint.setExpressFee(deliveryPointsEditParam.getExpressFee());
        placementPoint.setStatus(deliveryPointsEditParam.getStatus());
        placementPoint.setName(deliveryPointsEditParam.getName());
        placementPoint.setLongitude(deliveryPointsEditParam.getLongitude());
        placementPoint.setLatitude(deliveryPointsEditParam.getLatitude());
        placementPoint.setScenicSpotId(deliveryPointsEditParam.getServiceAreasId());
        customDeliveryPointMapper.updateById(placementPoint);
        return true;
    }

    @Override
    public DeliveryPointsResp list(int pageNum, int pageSize, String name) {
        DeliveryPointsResp deliveryPointsResp = new DeliveryPointsResp();
        pageNum = (pageNum - 1) * pageSize;
        int cnt = customDeliveryPointMapper.countList(name);
        if (cnt > 0) {
            deliveryPointsResp.setTotal(cnt);
            List<DeliveryPointPO> deliveryPointPOList = customDeliveryPointMapper.selectList(pageNum, pageSize, name);
            if (CollectionUtil.isNotEmpty(deliveryPointPOList)) {
                List<String> serviceIds = deliveryPointPOList.stream().map(DeliveryPointPO::getScenicSpotId).collect(Collectors.toList());
                List<IdAndNameResp> idAndNameRespList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(serviceIds)) {
                    idAndNameRespList = serviceAreaMapper.selectIdAndName(serviceIds);
                }

                List<DeliveryPointsResp.Record> recordList = new ArrayList<>();
                for (DeliveryPointPO deliveryPointPO : deliveryPointPOList) {
                    DeliveryPointsResp.Record record = BeanUtil.copyProperties(deliveryPointPO, DeliveryPointsResp.Record.class);
                    for (IdAndNameResp idAndNameResp : idAndNameRespList) {
                        if (idAndNameResp.getId().equals(record.getScenicSpotId())) {
                            record.setServiceAreasId(idAndNameResp.getId());
                            record.setServiceAreasName(idAndNameResp.getName());
                        }
                    }
                    recordList.add(record);
                }
                deliveryPointsResp.setRecordList(recordList);
            }
        }

        return deliveryPointsResp;
    }

    @Override
    public boolean updateStatus(IdRequest idRequest) {
        String id = idRequest.getId();
        PlacementPoint placementPoint = customDeliveryPointMapper.selectById(id);
        if (placementPoint == null) {
            throw new BusinessException("记录不存在");
        }
        int status = placementPoint.getStatus();
        if (status == 0) {
            placementPoint.setStatus(1);
        } else {
            placementPoint.setStatus(0);
        }
        return customDeliveryPointMapper.updateById(placementPoint) > 0;
    }
}
